package com.snail.billing.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.PaymentConst;
import com.snail.billing.PaymentParams;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonState;
import com.snail.billing.session.StateImprestSession;

public class StateImprestFragment extends Page implements View.OnClickListener, AdapterView.OnItemClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private View layout;
	private TextView textAccount;
	private ListView listView;
	private List<Map<String, Object>> data;
	private SimpleAdapter adapter;

	private StateImprestSession stateSession;
	private JsonState jsonState;
	
	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_state_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText("选择充值方式");
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		layout = findViewById(ResUtil.getViewId(Resource.id.snailbilling_state_layout));
		layout.setVisibility(View.GONE);
		textAccount = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_state_text_account));
		textAccount.setText("账号：" + DataCache.getInstance().importParams.account);
		listView = (ListView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_state_list_view));
		listView.setOnItemClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		requestPayState();
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		String index = jsonState.getIndexs()[position];
		int platformId = jsonState.getPlatforms().get(index).getPlatformId();
		String platformName = jsonState.getPlatforms().get(index).getName();
		DataCache.getInstance().paymentParams = new PaymentParams();
		DataCache.getInstance().paymentParams.platformId = platformId;
		DataCache.getInstance().paymentParams.platformName = platformName;
		if (platformId == PaymentConst.TYPE_ALIPAY_WEB
		//
				|| platformId == PaymentConst.TYPE_TENPAY
				//
				|| platformId == PaymentConst.TYPE_UPOMP2
				//
				|| platformId == PaymentConst.TYPE_ALIPAY_APP
				//
				|| platformId == PaymentConst.TYPE_WECHAT
		//
		) {
			getPageManager().forward(PayOneFragment.class);
		} else if (platformId == PaymentConst.TYPE_MOBILE_CARD) {
			getPageManager().forward(PayMobileCardFragment.class);
		} else if (platformId == PaymentConst.TYPE_SNAIL_CARD) {
			getPageManager().forward(PaySnailCardFragment.class);
		} else {
			AlertUtil.show(getContext(), "暂不支持该充值方式");
		}
	}

	private void requestPayState() {
		if (jsonState != null && jsonState.getPlatforms() != null) {
			listView.setAdapter(createAdapter());
			layout.setVisibility(View.VISIBLE);
		} else {
			HttpApp httpApp = new HttpApp(getContext());
			stateSession = new StateImprestSession();
			httpApp.setOnHttpResultListener(this);
			httpApp.request(stateSession);
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(stateSession)) {
				jsonState = new JsonState((String) stateSession.getResponseData());
				if (jsonState != null && jsonState.getPlatforms() != null) {
					listView.setAdapter(createAdapter());
					layout.setVisibility(View.VISIBLE);
				}
			}
		}
	}

	private SimpleAdapter createAdapter() {
		data = new ArrayList<Map<String, Object>>();
		for (String index : jsonState.getIndexs()) {
			JsonState.Platform platform = jsonState.getPlatforms().get(index);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(MyAdapter.TEXT, platform.getName());
			map.put(MyAdapter.TEXT2, "");
			int drawableId = ResUtil.getDrawableId(PaymentConst.IMAGE.get(platform.getPlatformId()));
			if (drawableId == 0) {
				drawableId = ResUtil.getDrawableId(Resource.drawable.snailpay_icon_empty);
			}
			map.put(MyAdapter.IMAGE, drawableId);
			data.add(map);
		}
		//
		int resource = ResUtil.getLayoutId(Resource.layout.snailbilling_state_item);
		String[] from = { MyAdapter.TEXT, MyAdapter.TEXT2 };
		int[] to = { ResUtil.getViewId(Resource.id.snailbilling_state_text), ResUtil.getViewId(Resource.id.snailbilling_state_text2) };
		adapter = new MyAdapter(getContext(), data, resource, from, to);
		return adapter;
	}

	private class MyAdapter extends SimpleAdapter {

		private static final String IMAGE = "image";
		private static final String TEXT = "text";
		private static final String TEXT2 = "text2";

		public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = super.getView(position, convertView, parent);
			//
			ImageView imageView = (ImageView) convertView.findViewById(ResUtil.getViewId(Resource.id.snailbilling_state_image));
			Object image = data.get(position).get(IMAGE);
			if (image != null) {
				if (image instanceof Integer) {
					int resId = (Integer) image;
					imageView.setImageResource(resId);
				} else if (image instanceof String) {
					String filePath = (String) image;
					Bitmap bitmap = BitmapFactory.decodeFile(filePath);
					imageView.setImageBitmap(bitmap);
				}
			}
			//
			Object text2 = data.get(position).get(TEXT2);
			if (TextUtils.isEmpty((String) text2)) {
				View view = convertView.findViewById(ResUtil.getViewId(Resource.id.snailbilling_state_text2));
				view.setVisibility(View.GONE);
			}
			return convertView;
		}
	}
}
